/**
 * 
 */
package com.aqarat.managedBean;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.core.managedBean.BaseBean;
import com.core.util.Utils;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class CalculatePaymentsBean extends BaseBean{
	private static final long serialVersionUID = 1L;
	private Double salePrice;
	private Double firstPayment;
	private Long noOfPayments;
	// -----------------------------//
	private Long totalNoOfPayments;
	private Double paymentValue;
	private Double remainingAmount;

	@PostConstruct
	public void loadData() {
	}

	public void calculatePaymentValue() {
		if(Utils.isEmpty(salePrice) || Utils.isEmpty(firstPayment) || Utils.isEmpty(noOfPayments)){
			super.addErrorMessage("You have To Enter All Fields");
			return;
		}
		this.paymentValue = (salePrice - firstPayment) / noOfPayments;
//		Double paymentValue = (salePrice - firstPayment) / noOfPayments;

//		totalNoOfPayments = Math.round(numberOfPayments);
//		this.noOfPayments = (long) Math.rint(numberOfPayments);
//		this.remainingAmount = (salePrice - firstPayment) % paymentValue;

	}
	
	public void calculateNumberOfPayments() {
		if(Utils.isEmpty(salePrice) || Utils.isEmpty(firstPayment) || Utils.isEmpty(paymentValue)){
			super.addErrorMessage("You have To Enter All Fields");
			return;
		}
		
		Double numberOfPayments = (salePrice - firstPayment) / paymentValue;
		
		totalNoOfPayments = Math.round(numberOfPayments);
		this.noOfPayments = (long) Math.rint(numberOfPayments);
		this.remainingAmount = (salePrice - firstPayment) % paymentValue;
		
	}

	public final Double getSalePrice() {
		return salePrice;
	}

	public final void setSalePrice(Double salePrice) {
		this.salePrice = salePrice;
	}

	public final Double getFirstPayment() {
		return firstPayment;
	}

	public final void setFirstPayment(Double firstPayment) {
		this.firstPayment = firstPayment;
	}

	public final Long getNoOfPayments() {
		return noOfPayments;
	}

	public final void setNoOfPayments(Long noOfPayments) {
		this.noOfPayments = noOfPayments;
	}

	public final Long getTotalNoOfPayments() {
		return totalNoOfPayments;
	}

	public final void setTotalNoOfPayments(Long totalNoOfPayments) {
		this.totalNoOfPayments = totalNoOfPayments;
	}

	public final Double getPaymentValue() {
		return paymentValue;
	}

	public final void setPaymentValue(Double paymentValue) {
		this.paymentValue = paymentValue;
	}

	public final Double getRemainingAmount() {
		return remainingAmount;
	}

	public final void setRemainingAmount(Double remainingAmount) {
		this.remainingAmount = remainingAmount;
	}

}
